#!/usr/bin/python

'''
argv[1]: key | data | get
argv[2]: keyspace name
'''

import os, sys, time
import pycassa
import pycassa.system_manager
from google.appengine.datastore import entity_pb
from protoBuff_decode import *

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'Usage: ' + sys.argv[0] + '  (key | data | get)'
        sys.exit(0)
    elif  sys.argv[1].lower() == 'get':
        if len(sys.argv) < 3:
            print 'Usage: ' + sys.argv[0] + '  get  your_key'
            sys.exit(0)
    
    db_operation_str = sys.argv[1]
    
    
    cassandra_7_srv_addr = '128.111.55.223'
    cassandra_7_port = 29160
    keyspace_name = sys.argv[2]
    column_family_name = "Standard1"
    
    # get data form cassandra 0.6
    print '======================================================================'
    print 'Connecting to ' + cassandra_7_srv_addr + ':' + str(cassandra_7_port)
    cass_7_pool = pycassa.connect(keyspace_name, [cassandra_7_srv_addr + ':' + str(cassandra_7_port)])
    print 'Connected!'
    
    cass_7_col_fam = pycassa.ColumnFamily(cass_7_pool, 'Standard1')    
    print '======================================================================'

    if db_operation_str.lower() == 'key' or db_operation_str.lower() == 'data':
        all_column_family_list = list(cass_7_col_fam.get_range("", ""))  
        cf_counter = 0
        for one_cf in all_column_family_list:
            cf_counter += 1
            column_key = one_cf[0]
            columns = one_cf[1]
            #print 'key[' + str(cf_counter) + '] : ' + column_key
            if db_operation_str.lower() == 'data': 
                print 'Columns:'
                col_counter = 0
                for col_name, col_val in columns.items():
                    col_counter += 1
                    col_val_str = col_val
                    if col_name == 'Encoded_Entity':
                        # decode Encoded_Entity
                        result_dict = {}
                        entity = entity_pb.EntityProto(col_val)
                        for property in entity.property_list() + entity.raw_property_list():
            
                            value_name = property.name()
                            
                            value_infos = property.value()
                            value_type, value_val = decode_value(value_infos)
                            
                            if value_type == "user":
                                if value_val.has_key('email'):
                                    result_dict[value_name] = str(value_val['email'])
                                else:
                                    result_dict[value_name] = ""
                            else:
                                result_dict[value_name] = value_val
                        col_val_str = '\n            (decoded by protocol buffer)\n'
                        for k, v in result_dict.items():
                            col_val_str += '            ' + str(k) + ' : ' + str(v) + '\n'
                    
                    print '   Column ' + str(col_counter) + ':'
                    print '      name : ' + col_name
                    print '      value: ' + col_val_str
    print 'key number: ' + str(cf_counter)  
  
    if db_operation_str.lower() == 'get':
        column_key = sys.argv[2]
        print 'key : ' + column_key
        print 'Columns:'
        columns = cass_7_col_fam.get(column_key)
        
        col_counter = 0
        for col_name, col_val in columns.items():
            col_counter += 1
            col_val_str = col_val
            if col_name == 'Encoded_Entity':
                # decode Encoded_Entity
                result_dict = {}
                entity = entity_pb.EntityProto(col_val)
                for property in entity.property_list() + entity.raw_property_list():
    
                    value_name = property.name()
                    
                    value_infos = property.value()
                    value_type, value_val = decode_value(value_infos)
                    
                    if value_type == "user":
                        if value_val.has_key('email'):
                            result_dict[value_name] = str(value_val['email'])
                        else:
                            result_dict[value_name] = ""
                    else:
                        result_dict[value_name] = value_val
                col_val_str = '\n            (decoded by protocol buffer)\n'
                for k, v in result_dict.items():
                    col_val_str += '            ' + str(k) + ' : ' + str(v) + '\n'
            
            print '   Column ' + str(col_counter) + ':'
            print '      name : ' + col_name
            print '      value: ' + col_val_str
    
    if db_operation_str.lower() == 'db':
        sys_manage = pycassa.system_manager.SystemManager(cassandra_7_srv_addr + ':' + str(cassandra_7_port))
        print 'Cluster Name  : ' + sys_manage.describe_cluster_name()
        print '----------------------------------------------------------------------'
        print 'Server API Ver: ' + sys_manage.describe_version()
        print '----------------------------------------------------------------------'
        print 'Partitioner   : ' + sys_manage.describe_partitioner()
        print '----------------------------------------------------------------------'
        ring_infos = sys_manage.describe_ring(keyspace_name)
        print 'Describe Ring : '
        ring_counter = 0
        for ring_info in ring_infos:
            ring_counter += 1
            print '    Ring[' + str(ring_counter) + '] : '
            print '        start_token : ' + ring_info.start_token
            print '        end_token   : ' + ring_info.end_token
            print '        endpoints   : '
            for endpoint in ring_info.endpoints:
                print '              ' + endpoint
        
    print '----------------------------------------------------------------------'
    
    
    print 'Done!'
    
    
    
    
    
